<?php if ( !defined('BASEPATH') ) exit('No direct script access allowed');
/** 
* Loto Class 
* 
*/
class Event_loto extends CI_Controller {

	// --------------------------------------------------------------------
	/** 
	* init function
	* 
	* @access private
	* @param null
	* @return null
	*/
	function __construct(){
        parent::__construct();

		$this->load->library('smarty3', '', 'smarty');
        
        require_once(APPPATH . 'modules/backend/autoload.php');        
        
        $this->load->library('login_manager'); 
        require_once(APPPATH . 'modules/backend/info.php');

        $this->login_manager->check_role('menu');
        $this->load->language('backend');
        
        $this->smarty->assign('language', $this->lang);
        $this->smarty->assign('message', $this->session->flashdata('message'));
        $this->smarty->assign('error_message', $this->session->flashdata('error_message'));
		//$this->output->enable_profiler(TRUE);
	}
	
    function index() {
        //echo "abc";exit;
        $this->history();
        $this->smarty->view('backend/event_loto/history_detail', 'main_content');
        $this->smarty->display('template/backend/admin');
    }
    
    function history(){
    	$this->load->model('event/loto_history_model');
    	$this->load->model('event/loto_item_model');
    	$this->load->model('event/loto_customer_model');
        
        $_input['week']    		= $this->input->get("week");
        $_input['item_id']      = $this->input->get("item_id");
        $_input['username']     = $this->input->get("username");     
        $_input['status']       = $this->input->get("status");
        
        // view
        $this->smarty->assign('_input', $_input);        
        $this->smarty->assign('loto_item', $this->loto_item_model); 
        $this->smarty->assign('loto_history', $this->loto_history_model);
        $this->smarty->assign('loto_customer', $this->loto_customer_model);        
        
    }

	function item() {
		$action = $this->uri->rsegment(3);
		
		switch ($action){
			case "jqgrid_jsoninit":
				$this->item_jqgrid_jsoninit();
			break;
			case "jqgrid_jsondata":
				$this->item_jqgrid_jsondata();
			break;
			case "edit":
				$this->item_edit();
			break;
		}
		
		$parameters = $this->uri->ruri_to_assoc();
	
		if (isset($parameters['id'])) {
			$id = intval($parameters['id']);
			if ($id > 0) {
				$this->session->set_userdata('search_id', $id);
			}
		}
	
		$css_files = array(base_url() . 'data/jscripts/jqgrid/css/ui.jqgrid.css');
	
		$js_files = array(
				base_url() . 'data/jscripts/jqgrid/i18n/grid.locale-vi.js',
				base_url() . 'data/jscripts/jqgrid/jquery.jqGrid.min.js',
		);
	
		require_once(APPPATH . 'modules/backend/history.php');
		$this->load->model('event/loto_item_reward_model');
	
		//load parameters and initialize grid
		$jqgrid = array();
	
		$jqgrid['id'] = 'jqgrid';
		$jqgrid['pager'] = 'pagered';
		$jqgrid['caption'] = $this->loto_item_reward_model->localize_label('manager');
		$jqgrid['url_metadata'] = site_url($this->router->directory . 'event_loto/item/jqgrid_jsoninit');
		$jqgrid['url_data'] = site_url($this->router->directory . 'event_loto/item/jqgrid_jsondata');
		$jqgrid['url_edit'] = site_url($this->router->directory . 'event_loto/item/edit');
	
		$this->smarty->assign('jqgrid', $jqgrid);
		$jqgrid_init = $this->smarty->get_contents('template/backend/js/jqgrid_edit_view');
	
		$js_text = array($jqgrid_init);
		$this->smarty->assign('css_files', $css_files);
		$this->smarty->assign('js_files', $js_files);
		$this->smarty->assign('js_text', $js_text);
		$this->smarty->assign('title', $this->loto_item_reward_model->localize_label('manager'));
	
		// view
		$this->smarty->view('backend/event_loto/jqgrid', 'grid');
		$this->smarty->display('template/backend/admin');
	}
	
	public function item_edit() {
		$this->load->model('event/loto_item_reward_model');
	
		if ($this->input->server('REQUEST_METHOD') === 'GET') {
	
			$parameters = $this->uri->ruri_to_assoc(4);
	
			if (isset($parameters['id'])) {
				$id = intval($parameters['id']);
			} else {
				$id = 0;
			}

			$this->loto_item_reward_model->get_by_id($id);
			
			if ($this->loto_item_reward_model->exists()) {
				$this->smarty->assign('error', '');
				$this->smarty->assign('loto_item_reward', $this->loto_item_reward_model);
				$this->smarty->view('backend/event_loto/item_edit', 'main_content');
				$this->smarty->display('template/backend/admin');
			} else {
				$this->session->set_flashdata('error_message', $this->lang->line('not_found')/* 'Bài viết này không tồn tại xin vui lòng kiểm tra lại.' */);
				redirect(site_url('auth/event_loto/item'));
			}
		} else {
			$this->load->library('form_validation');
			$this->form_validation->set_rules('percent', $this->lang->line('percent'), 'required');
	
			$id = $this->input->post('id');
			$this->loto_item_reward_model->get_by_id($id);
	
			if ($this->form_validation->run() == TRUE) {				
					$this->_save(site_url('auth/event_loto/item'), $this->loto_item_reward_model, $result);
			} else {
				$this->smarty->assign('loto_item_reward', $this->loto_item_reward_model);
				$this->smarty->view('backend/event_loto/item_edit', 'main_content');
				$this->smarty->display('template/backend/admin');
			}
		}
		exit();
	}
	
	private function _save($uri, $data, $result = FALSE) {
	
		// Begin transaction
		$data->trans_begin();
		$this->load->helper('news');
	
		$data->percent = $this->input->post('percent');		
	
		$data->save();
		$exists = $data->exists();
		// Attempt to save user
		// Check status of transaction
		if ($data->trans_status() === FALSE) {
			// Transaction failed, rollback
			$data->trans_rollback();
	
			// Add error message
			$u->error_message('transaction', 'The transaction failed to save (insert)');
		} else {
			// Transaction successful, commit
			$data->trans_commit();
	
			if ($exists) {
				$this->session->set_flashdata('message', $data->localize_label('edit_success') /* 'Đã cập nhật dữ liệu thành công.' */);
			} else {
				$this->session->set_flashdata('message', $data->localize_label('create_success') /* 'Đã lưu dữ liệu thành công.' */);
			}
			redirect($uri);
		}
		// Show all errors
		$error = $data->error->string;
	
		$this->session->set_flashdata('message', $error);
		redirect($uri);
		exit();
	}
	
	function item_jqgrid_jsoninit() {
		if (IS_AJAX) {
			$this->load->model('event/loto_item_reward_model');
			$metadata = $this->loto_item_reward_model->jqgrid_metadata();
			echo json_encode($metadata);
		}
		exit();
	}
	
	/**
	 * data function, get data for jquery grid via ajax
	 *
	 * @access public
	 * @param null
	 * @return array in json encode
	 */
	function item_jqgrid_jsondata() {
		if (IS_AJAX) {
			$search = $this->input->post('_search');
			$filters = json_decode($this->input->post('filters'));
			$order = $this->input->post('sidx');
			$order_direct = $this->input->post('sord');
			$page = intval($this->input->post('page'));
			$num_per_page = intval($this->input->post('rows'));
	
			$relative_search = array(
					'user' => array(
							'search' => 'id'
					)
			);
	
			$this->load->helper('jqgrid');
			$this->load->model('event/loto_item_reward_model');
			$data = jqgrid_search($this->loto_item_reward_model, $search, $filters, $order, $order_direct, $page, $num_per_page, $relative_search);
	
			$paged = $data->paged;
			$responce = new Stdclass();
			$responce->page = $paged->current_page; //curent page
			$responce->total = $paged->total_pages; //total page
			$responce->records = $paged->total_rows; //total record
			$i = 0;
	
			$responce = new Stdclass();
			$responce->page = $paged->current_page; //curent page
			$responce->total = $paged->total_pages; //total page
			$responce->records = $paged->total_rows; //total record
			$i = 0;
	
			foreach ($data as $item) {
				$isLeaf = TRUE;
				$responce->rows[$i]['cell'] = array(
						// Field
						$item->id,
						$item->get_item_name(),
						$item->percent,
						NULL,
						NULL,
						$isLeaf,
						false
				);
				$i++;
			}
			echo json_encode($responce);
			exit();
		}
	}
}
/* End of file dial.php */
/* Location: ./system/application/controllers/backend/dial.php */